module FilmManager (insertFilm, deleteFilm, updateFilm, searchFilm) where

import Film
import DBS
import StringHelper
import IdObject
import Date
import qualified Loan

-- umieszcza film w bd-
insertFilm :: FilmInfo -> DBS -> DBS
insertFilm as db = setLastFilmId i dbb where
    dbb = setFilms oss db
    oss = os ++ [o]
    os = getFilms db
    o   = Film i as
    i = 1 + getLastFilmId db

-- usuwa film z danym ID z db. Jak zle id, zwraca informacje o bledzie
deleteFilm :: ID -> DBS -> Either DBS String
deleteFilm i db	| (selectFilmBy ((== i).getId) db /= [])	= 
                        if (selectLoanBy ((== i) . (Loan.getFilmId)) db /= []) 
                        then Right "znaleziono wypozyczenie odwolujace sie do tego filmu"
                        else Left (DBS (getLastFilmId db) (IdObject.remove i (getFilms db)) 
                                       (getLastClientId db) (getClients db) 
                                       (DBS.getLastLoanId db) (getLoans db))
		| otherwise					= Right "nie znaleziono filmu o podanym id"

{- aktualizuje:
	- atrybut Filmu o danym ID
	- w bazie danych DBS
	- atrybut o nr ID
	- ustawia wartosc val

	jak zle ID, zwraca Nothing
-}
updateFilm :: ID -> DBS -> Int -> String -> Either DBS String
updateFilm idFilm db idParam newVal	
	| (idParam > 6 || idParam < 1)			= Right "podano bledny numer atrybutu"
	| ((get idFilm (getFilms db)) /= Nothing)	= 
                if (idParam==2 || idParam==5)
                then maybeDb
                else Left (setFilms fs2 db)
	| otherwise					= Right "nie znaleziono filmu o podanym id"
	where	fs = selectFilmBy ((/= idFilm).getId) db
		[f] = selectFilmBy ((== idFilm).getId) db
                maybeInt = string2int newVal
                maybeDb = case maybeInt of
                        Just _  -> Left (setFilms fs2 db)
                        Nothing -> Right "podano wartosc nie bedaca liczba dla atrybutu 'rok produkcji' lub 'stan'"
		fs2 = fs ++ [film]
			where film	| idParam == 1	= (setTitle f newVal)
					| idParam == 2	= case maybeInt of
                                                                Just intVal  -> (setYear f intVal)
					| idParam == 3	= (setDirector f newVal)
					| idParam == 4	= (setKind f newVal)
					| idParam == 5	= case maybeInt of
                                                                Just intVal  -> (setState f intVal)
					| otherwise	= (setDescription f newVal)
                                        
-- funkcja prywatna - wyszukiwanie w kazdym filmie z listy Stringa
searchFilmH :: String -> [Film] -> [Film]
searchFilmH s [] = []
searchFilmH s (f:fs)	| isPartOf s (getTitle f)
				|| isPartOf s (getDirector f)
				|| isPartOf s (getKind f)
                                || isPartOf s (show(Film.getYear f))	= f : searchFilmH s fs
			| otherwise					= searchFilmH s fs

-- szuka Stringa w tytule, rezyserze, roku i gatunku, zwraca liste pasujacych filmow
searchFilm :: String -> DBS -> [Film]
searchFilm [] db = getFilms db
searchFilm s db = searchFilmH s (getFilms db)
